/*
 * @Description: 你的代码我的心
 * @Author: GGB
 * @Date: 2021-12-27 09:57:24
 * @LastEditors: GGB
 * @LastEditTime: 2022-01-26 15:08:36
 */

import { FormSchema } from '/@/components/Table';
import { getApiList } from '/@/api/permission/api';

const mapTree = (org) => {
  const haveChildren = Array.isArray(org.children) && org.children.length > 0;
  return {
    //分别将我们查询出来的值做出改变他的key
    title: org.name,
    value: org.api_id,
    key: org.api_id,
    //判断它是否存在子集，若果存在就进行再次进行遍历操作，知道不存在子集便对其他的元素进行操作
    children: haveChildren ? org.children.map((i) => mapTree(i)) : [],
  };
};
// TODO 这里有一个bug 通过接口获取树形数据--并做适配处理
const getApiListCustom = (params) =>
  new Promise(async (resolve, rejects) => {
    const data = await getApiList(params);
    if (!data) rejects([]);
    // 对list进行递归处理
    const list = data.list as any[];
    // 新增顶级处理
    list.unshift({
      name: '顶级权限',
      api_id: 0,
    });
    const newList = list.map((org) => mapTree(org));
    resolve(newList);
  });

// 库存输入框
export const formSchemas: FormSchema[] = [
  {
    field: 'name',
    component: 'Input',
    label: '权限名称',
    colProps: {
      span: 24,
    },
    required: true,
    componentProps: {
      placeholder: '请输入权限名称',
    },
  },
  {
    field: 'parent_id',
    component: 'ApiTreeSelect',
    label: '上级权限',
    defaultValue: 0,
    required: true,

    componentProps: {
      api: getApiListCustom,
      resultField: 'list',
      // TODO拉完全部
      params: {
        pageSize: 500,
      },
    },
    colProps: {
      span: 24,
    },
  },
  {
    field: 'url',
    component: 'Input',
    label: '权限url',
    colProps: {
      span: 24,
    },
    required: true,
    componentProps: {
      placeholder: '权限url',
    },
  },
  {
    field: 'sort',
    component: 'Input',
    label: '排 序',
    colProps: {
      span: 24,
    },
    rules: [{ required: true }],
    defaultValue: 100,
    componentProps: {
      placeholder: '请输入账号',
    },
    helpMessage: '数字越小越靠前',
  },
];
